package leencode;

import org.junit.jupiter.api.Test;

public class no27_移除元素 {

    @Test
    public void test(){
        int[] ints1 = new int[5];

        int[] ints = {3,2,2,3};
        removeElement1(ints,3);

        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }
    public static int removeElement1(int[] nums, int val) {
        int cur = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val){
                nums[cur++] = nums[i];
            }

        }

        return cur;
    }


        public static int removeElement2(int[] nums, int val) {
        int[] res = new int[nums.length];
        int k = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] != val){
                res[k++] = nums[i];
            }
        }

        for (int i = 0; i < nums.length; i++) {
            if (i < k){
                nums[i] = res[i];
            } else {
                nums[i] = 0;
            }
        }


        return k+1;
    }

}
